package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * 搜附近服务接口实现类
 *
 */
@Service
public class LocationApiImpl implements LocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 上报地址位置
     * @param latitude 纬度
     * @param longitude 经度
     * @param addrStr
     * @param userId
     */
    @Override
    public void saveLocation(Double latitude, Double longitude, String addrStr, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        //1.查询当前用户地址位置是否存在
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //2.如果不存在则保存地址位置
        long nowTime = System.currentTimeMillis();
        if(userLocation == null){
            userLocation = new UserLocation();
            userLocation.setId(ObjectId.get());//主键id
            userLocation.setAddress(addrStr);//地址位置
            userLocation.setUserId(userId);//当前用户id
            userLocation.setCreated(nowTime);
            userLocation.setUpdated(nowTime);
            userLocation.setLastUpdated(nowTime);
            //x表示当地的纬度，y表示当地的经度
            userLocation.setLocation(new GeoJsonPoint(latitude,longitude));
            mongoTemplate.insert(userLocation);
        }else {
            //3.如果存在则更新地址位置
            Update update = new Update();
            update.set("location",new GeoJsonPoint(latitude,longitude));
            update.set("address",addrStr);
            update.set("updated",nowTime);
            update.set("lastUpdated",nowTime);
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }

    }

    /**
     * 根据当前用户位置 与 需要搜索距离 查询附近的用户数据
     * @param distance
     * @param userId
     * @return
     */
    @Override
    public List<UserLocationVo> searchNearUser(Long distance, Long userId) {
        //1.根据当前登录的用户id查询当前用户位置
        Query myQuery = new Query();
        myQuery.addCriteria(Criteria.where("userId").is(userId));//当前用户的id
        UserLocation userLocation = mongoTemplate.findOne(myQuery, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation();//获取当前用户的位置
        //2.调用mongodb中api 传入当前用户位置 搜索距离 完成附近用户数据查询
        Query nearQuery = new Query();
        //2.1创建圆对象  当前用户的位置 搜索的距离
        Circle circle = new Circle(location,new Distance(distance/1000, Metrics.KILOMETERS));
        nearQuery.addCriteria(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(nearQuery, UserLocation.class);
        //3.将附近的用户数据返回
        return UserLocationVo.formatToList(userLocationList);
    }
}
